---
--类名: PhotoViewShare.lua
--创建日期: 2018-7-30
--创建人: 宋建邦
--功能描述:分享视图
--修改内容:
--修改日期:
--修改人:
--修改备注:


local PhotoViewShare = class("PhotoViewShare", app.mvc.ViewBase)
PhotoViewShare.VIEW_ROOT_RES = Res.SceneSharePhoto

function PhotoViewShare:init()
    self:setNodeEventEnabled(true)
    self.snapshotImg = self._root:findChild("Panel/Image")
    self.snapshotImgBg = self._root:findChild("Panel/Image/bg")
    local sz = self.snapshotImg:getContentSize()
    local nsz = cc.size(sz.width, sz.width*(display.height/display.width))
    self.snapshotImg:setContentSize(nsz)

    -- self.snapshotImg:setContentSize(cc.size(display.width, display.height))
    -- self.snapshotImg:setScale(display.width/nsz.width)

    self._root:findChild("Panel"):setContentSize(display.safe_size)
    display.doLayout(self._root)

    self.pctl = app:getInst("PhotoController")
    self.snapshotImg:setScale9Enabled(false)
    local path = self.pctl:getSnapShotPath()
    cc.Director:getInstance():getTextureCache():reloadTexture(path)
    self.snapshotImg:loadTexture(path)

    local rd = self.snapshotImg:getVirtualRenderer()
    local osz = rd:getOriginalSize()
    local fsz = self.snapshotImg:getContentSize()
    local bq = self._root:findChild("Panel/btn_1")
    -- if bq == nil then
    --     bq = self._root:findChild("Panel/btn_fb")
    -- end
    if bq then
        fsz.width = (bq:getPositionX() - bq:getContentSize().width*0.5 - display.safe_width * 0.5) * 2 - 6
        fsz.height = display.safe_height
    
        if osz.width/osz.height > fsz.width/fsz.height then
            self.snapshotImg:setContentSize(cc.size(fsz.width, fsz.width * osz.height/osz.width))
            self.snapshotImgBg:setContentSize(cc.size(fsz.width, fsz.width * osz.height/osz.width))
        else
            self.snapshotImg:setContentSize(cc.size(fsz.height * osz.width/osz.height, fsz.height))
            self.snapshotImgBg:setContentSize(cc.size(fsz.height * osz.width/osz.height, fsz.height))
        end
    end
   
    
    display.doLayout(self._root)
    self.snapshotImg:stopAllActions()
    self.snapshotImg:runAction(cc.ScaleTo:create(0.6, 1))
    self:initButtons()
    self:initAction()

    -- self._root:findChild("Panel/share"):hide()

    UIHandler:hideDesktop()
end


function PhotoViewShare:onExit()
    UIHandler:showDesktop()
end

function PhotoViewShare:initAction()
    if not self.splash then
        self.splash = ccui.Layout:create()
        self.splash:setBackGroundColor(display.COLOR_WHITE)
        self.splash:setBackGroundColorOpacity(255)
        self.splash:setBackGroundColorType(1)
        self.splash:setContentSize(cc.size(display.width, display.height))
        self.splash:setAnchorPoint(0, 0)
        self:addChild(self.splash)
        self.splash:hide()
    end

    local act = cca.seq({cca.delay(0), cca.callFunc(function()
        self.splash:show()
        self.splash:setOpacity(255)
        self.splash:setBackGroundColorOpacity(255)
    end), 
    cca.fadeIn(0.1),cca.fadeOut(0.5), cca.hide()})

    self.splash:runAction(act)
end

function PhotoViewShare:initButtons()
    local shareConfig = {}
    cc.SpriteFrameCache:getInstance():addSpriteFrames('ui_resources/main1.plist')
    if app:getPublisher() == Enums.Publisher.ema then
        shareConfig = {
            {imagePath = Res.ShareIcons.FaceBook, callback = function()
                app.channelAPI:share(nil, Enums.ShareType.facebook, L("fb_share_title"), L("fb_share_desc"), self.pctl:getSnapShotPath())
            end},
            {imagePath = Res.ShareIcons.Twitters, callback = function()
                app.channelAPI:share(nil, Enums.ShareType.twitter, L("twt_share_title"), L("twt_share_desc"), self.pctl:getSnapShotPath())
            end},
        }
    elseif app:getPublisher() == Enums.Publisher.tencent then
        local wgPlatform = app.channelAPI:getWGPlatform()
        if wgPlatform == Enums.MSDK_PLATFORM.QQ  then
            shareConfig = {
                {imagePath = Res.ShareIcons.QQ, callback = function()
                    
                    if not app.channelAPI:isPlatformInstalled(Enums.MSDK_PLATFORM.QQ) then
                        display.pushToast(L("QQNotInstalledForShare"))
                        return
                    end
                    
                    app.channelAPI:share(nil, Enums.ShareType.qqSession, L("title"), L("desc"), self.pctl:getSnapShotPath())
                    self:_closePhotoView()
                end},
                {imagePath = Res.ShareIcons.QQZone, callback = function()
                    
                    if not app.channelAPI:isPlatformInstalled(Enums.MSDK_PLATFORM.QQ) then
                        display.pushToast(L("QQNotInstalledForShare"))
                        return
                    end
                
                    app.channelAPI:share(nil, Enums.ShareType.qqZone, L("title"), L("desc"), self.pctl:getSnapShotPath())
                    self:_closePhotoView()
                end},
            }
        elseif wgPlatform == Enums.MSDK_PLATFORM.WX then
            shareConfig = {
                {imagePath = Res.ShareIcons.WX, callback = function()
                   
                    if not app.channelAPI:isPlatformInstalled(Enums.MSDK_PLATFORM.WX) then
                        display.pushToast(L("WXNotInstalledForShare"))
                        return
                    end
                    
                    app.channelAPI:share(nil, Enums.ShareType.wxSession, L("title"), L("desc"), self.pctl:getSnapShotPath())
                    self:_closePhotoView()
                end},
                {imagePath = Res.ShareIcons.WXZone, callback = function()
                
                    if not app.channelAPI:isPlatformInstalled(Enums.MSDK_PLATFORM.WX) then
                        display.pushToast(L("WXNotInstalledForShare"))
                        return
                    end
                
                    app.channelAPI:share(nil, Enums.ShareType.wxCircle, L("title"), L("desc"), self.pctl:getSnapShotPath())
                    self:_closePhotoView()
                end},
            }
        end
    end

    for i = 1, 4 do
        local btn = self._root:findChild("Panel/btn_"..i)
        if btn then
            local cf = shareConfig[i]
            if cf then
                btn:loadTextures(cf.imagePath, cf.imagePath, cf.imagePath, 1)
                btn:addClickEventListener(function(sender)
                    cf.callback()
                end)
            else
                btn:setVisible(false)
            end
        end
    end


    self.closeBtn = self._root:findChild("Panel/Button_close")
    self.closeBtn:addClickEventListener(handler(self, self._closePhotoView))

end

function PhotoViewShare:_closePhotoView(sender)
    local sp = self.fromePhotoView
    local cfg = self.photoConfig
    app:getInst("PhotoController"):closePhotoViewShare()
    if sp then
        app:getInst("PhotoController"):showPhotoView(cfg)     
    end
end

function PhotoViewShare:getViewType()
	return app.mvc.VIEWTYPE_BLUR
end

return PhotoViewShare